package top.vybfi.week2.customer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.vybfi.week2.exception.ApiRequestException;

import javax.validation.Valid;
import java.util.List;

//控制层

@RequestMapping(path = "api/V2/user")
@RestController
public class CustomerControllerV2 {
    private final CustomerService customerService;

    @Autowired
    public CustomerControllerV2(CustomerService customerService) {
        this.customerService = customerService;
    }

    //增删改查

    @GetMapping
    List<User> getusers(){
        System.out.println("GET请求1...");
        return customerService.getusers();
    }

    @GetMapping(path = "{userId}")
    User getuser(@PathVariable("userId") long id){
        System.out.println("GET请求2...");
        return customerService.getuser(id);
    }

    ;
    @GetMapping(path = "{userId}/exception")
    User getCustomerException(@PathVariable("userId") long id){
        throw new ApiRequestException(
                "ApiRequestException for user" + id
        );
    }

    @PostMapping
    void createNewUser(@Valid @RequestBody User user){
        System.out.println("POST请求...");
        System.out.println(user);
    }

    @PutMapping
    void updateUser(@RequestBody User user){
        System.out.println("UPDATE请求...");
        System.out.println(user);
    }

    @DeleteMapping(path = "{userId}")
    void deleteUser(@PathVariable("userId") long id){
        System.out.println("DELETE请求...\n"+id);
    }
}
